package com.lemon.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lemon.domain.PageResult;
import com.lemon.domain.WarehousingEntryDetails;
import com.lemon.domain.WarehousingEntryUp;
import com.lemon.mapper.WarehousingEntryDetailsMapper;
import com.lemon.mapper.WarehousingEntryUpMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lemon.domain.WarehousingEntryDetailsSort;
import com.lemon.mapper.WarehousingEntryDetailsSortMapper;
import com.lemon.service.WarehousingEntryDetailsSortService;
@Service
@RequiredArgsConstructor
public class WarehousingEntryDetailsSortServiceImpl extends ServiceImpl<WarehousingEntryDetailsSortMapper, WarehousingEntryDetailsSort> implements WarehousingEntryDetailsSortService{
    private final WarehousingEntryDetailsMapper entryDetailsMapper;
    private final WarehousingEntryDetailsSortMapper entryDetailsSortMapper;
    private final WarehousingEntryUpMapper entryUpMapper;

    @Override
    public PageResult getPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<WarehousingEntryDetailsSort> warehouseAreaLocas = this.list();
        Page pageInfo = (Page) warehouseAreaLocas;
        return new PageResult(pageInfo.getTotal(),pageInfo.getResult());
    }

    @Override
    public void add(Integer id, Integer count,Integer damageCount) {
        WarehousingEntryDetails entryDetails = entryDetailsMapper.selectById(id);
        entryDetails.setStatus(4);
        entryDetailsMapper.updateById(entryDetails);
        WarehousingEntryDetailsSort sort = new WarehousingEntryDetailsSort();
        sort.setWarehousingEntryDetailsId(Long.valueOf(id));
        sort.setSkuId(entryDetails.getSkuId());
        sort.setStatus(1);
        sort.setCount(count);
        sort.setDamageCount(damageCount);
        sort.setCreateTime(LocalDateTime.now());
        entryDetailsSortMapper.insert(sort);
    }

    @Override
    public Integer getCount(Long warehousingEntryDetailsId) {
        List<WarehousingEntryDetailsSort> sortList = entryDetailsSortMapper.selectList(Wrappers.<WarehousingEntryDetailsSort>lambdaQuery().eq(WarehousingEntryDetailsSort::getWarehousingEntryDetailsId, warehousingEntryDetailsId));
        List<WarehousingEntryUp> upList = entryUpMapper.selectList(Wrappers.<WarehousingEntryUp>lambdaQuery().eq(WarehousingEntryUp::getEntryDetailsId, warehousingEntryDetailsId));
        int count = 0;
        if (CollectionUtils.isNotEmpty(sortList)){
            for (WarehousingEntryDetailsSort sort : sortList) {
                count += sort.getCount();
                count -= sort.getDamageCount();
            }
        }
        if (CollectionUtils.isNotEmpty(upList)){
            for (WarehousingEntryUp up : upList) {
                count -= up.getCount();
            }
        }
        return count;
    }
}
